/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.ohos.rajawali3d.examples;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.data.RajawaBean;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;

import java.util.List;

/**
 * contactItemProvider
 */
public class RajaItemProvider extends BaseItemProvider {
    private static final int DIALOG_WIDTH = 900;

    private static final int DIALOG_HEIGHT = 600;

    private static final int DIALOG_CORNER_RADIUS = 30;

    private CommonDialog commonDialog;

    private List<RajawaBean> rajaBeans;

    private Context context;

    public RajaItemProvider(Context context, List<RajawaBean> rajaBeans) {
        this.rajaBeans = rajaBeans;
        this.context = context;
    }

    @Override
    public int getCount() {
        return rajaBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return rajaBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component container = getItemComponent(position);
        return container;
    }

    private Component getItemComponent(int position) {
        Component container = getComponent(position);
        return container;
    }

    private Component getComponent(int position) {
        RajawaBean rajawaBean = rajaBeans.get(position);
        Component container = null;
        if (rajawaBean.getType() == RajawaBean.ETypeCategory) {
            container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_category_view, null, false);
            Text name = (Text) container.findComponentById(ResourceTable.Id_text1);
            name.setText(rajawaBean.getName());
        } else if (rajawaBean.getType() == RajawaBean.ETypeExample) {
            container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_example_view, null, false);
            Text name = (Text) container.findComponentById(ResourceTable.Id_text1);
            name.setText(rajawaBean.getName());
            name.setClickedListener(component -> launchExampleAbility(rajawaBean));
        } else {
            container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item, null, false);
            Text name = (Text) container.findComponentById(ResourceTable.Id_name);
            Text tel = (Text) container.findComponentById(ResourceTable.Id_tel);
            Component btnEdit = container.findComponentById(ResourceTable.Id_btn_edit);
            name.setText(rajawaBean.getName());
            if (rajawaBean.getExampleClass() != null)
                tel.setText("class:" + rajawaBean.getExampleClass().getName());
            else
                tel.setText("");
            btnEdit.setClickedListener(component -> showContactDetailDialog(rajawaBean, position));
        }
        return container;
    }

    private void launchExampleAbility(RajawaBean rajawaBean) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("org.ohos.rajawali3d")
                .withAbilityName("org.ohos.rajawali3d.examples.ExamplesAbility")
                .build();
        intent.setOperation(operation);
        String cls_name = rajawaBean.getExampleClass().getName();
        intent.setParam(ExamplesAbility.EXTRA_EXAMPLE_CLASS, cls_name);
        intent.setParam(ExamplesAbility.EXTRA_EXAMPLE_TYPE, rajawaBean.getType());
        intent.setParam(ExamplesAbility.EXTRA_EXAMPLE_URL, rajawaBean.getUrl());
        intent.setParam(ExamplesAbility.EXTRA_EXAMPLE_NAME, rajawaBean.getName());
        intent.setParam(AExampleFragment.BUNDLE_EXAMPLE_URL, rajawaBean.getPath());
        this.context.startAbility(intent, 11);
    }

    private void showContactDetailDialog(RajawaBean rajawaBean, int position) {
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_layout, null, false);
        TextField name = (TextField) container.findComponentById(ResourceTable.Id_name);
        name.setText(rajawaBean.getName());
        TextField tel = (TextField) container.findComponentById(ResourceTable.Id_tel);
        if (rajawaBean.getExampleClass() != null)
            tel.setText("class:" + rajawaBean.getExampleClass().getName());
        else
            tel.setText("");
        Button btnOk = (Button) container.findComponentById(ResourceTable.Id_btn_ok);

        btnOk.setClickedListener(component -> saveEdit(name.getText(), tel.getText(), position));
        commonDialog = new CommonDialog(context);
        commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
        commonDialog.setContentCustomComponent(container);
        commonDialog.show();
    }

    private void saveEdit(String name, String tel, int position) {
        rajaBeans.get(position).setName(name);
        rajaBeans.get(position).setExampleClass(tel);
        notifyDataChanged();
        commonDialog.destroy();
    }

    /**
     * update list data
     *
     * @param data contactDevicesData list
     */
    public void update(List<RajawaBean> data) {
        rajaBeans = data;
        notifyDataChanged();
    }
}
